package com.mengxj;

import com.common.collection.Student;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 拼查询sql
 * select 字段 from 表明 where 条件
 *
 *
 * b表明：用class来获取
 * Class的getName返回的是全称限定名
 * Class的getSimpleName
 *
 * @author mengxj
 */
public class Question {

    public static void main(String[] args) throws Exception {
        Student student = new Student();
        student.setName("abc");
        student.setAge(12);
        String sql = createSql(student);
        System.out.println(sql);
    }

    /**
     * 1，fieldName 首字母小写
     * 2，如果条件是字符串，需要加上“'”
     * 3，java是userName，数据库是user_name，缺少转换
     * @param obj
     * @return
     * @throws Exception
     */
    private static String createSql(Object obj) throws Exception {
        // getUserName  setName
        Method[] methods = obj.getClass().getDeclaredMethods();
        List<String> columns = new ArrayList<>();
        List<String> wheres = new ArrayList<>();
        for(Method method: methods){
            String methodName = method.getName();
            if(methodName.startsWith("get")){
                //首字母应该小写
                String fieldName = methodName.substring(3);
                columns.add(fieldName);
                Object value = method.invoke(obj);
                if(Objects.nonNull(value)){
                    wheres.add(String.format("%s = %s", fieldName, value));
                }
            }
        }
        String columnSql = String.join(", ", columns);
        String whereSql = String.join(" and ", wheres);
        return String.format("select %s from %s where %s", columnSql, obj.getClass().getSimpleName(), whereSql);
    }

    static void test(){
        Question question = new Question();
        List<String> list = Arrays.asList("name", "age", "address");
        String columns = question.columns(list);
        System.out.println(columns);
        String tableName = question.table(question);
        System.out.println(tableName);
        String whereSql = question.whereSql();
        String sql = "select " + columns + " from " + tableName + whereSql;
        System.out.println(sql);
        String model = "select %s from %s %s";
        // %s 可以替换字符串（任何）  %d 代表数字  %f 浮点数
        String res = String.format("select %s from %s %s", columns, tableName, whereSql);
        System.out.println(res);
        System.out.println(String.format("aaa %d bbb %s", 1, "aa"));
        double num = 2.31;
        System.out.println(String.format("%.5f", num));
        String line = String.join(" , ", list);
        System.out.println(line);
    }

    /**
     * name, age, address
     *
     * @param list
     * @return
     */
    String columns(List<String> list){
        String column = "";
        for(int i = 0; i < list.size(); i ++){
            column += list.get(i);
            if(i != list.size() - 1){
                column += ", ";
            }
        }
        return column;
    }

    String table(Object obj){
        return obj.getClass().getSimpleName();
    }

    String whereSql(){
        //1, 反射调用get方法获取值
        //2，判断1是不是空
        //3，2成立的情况下，拼接 字段名 = 值
        return "where 1 = 1";
    }
}
